package shoot.client.core;

import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;

import core.World;
import shoot.clientInfo.ShootClientInfo;

public class ShootClient {
	public World world;;
	private Socket socket;
	private ShootClientInfo shootInfo;
	public ShootClient(){			
		try {
			System.out.println("正在连接服务器.......");	
			socket = new Socket("178.120.12.12",8088);
			System.out.println("与服务端连接成功.......");
			shootInfo=new ShootClientInfo();
			world=new World();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
	private int frequency = 10;
	public void start() {		
		try {					
			ObjectOutputStream oos= new ObjectOutputStream(socket.getOutputStream());
			ObjectInputStream ois=new ObjectInputStream(socket.getInputStream());		
			//定时器10毫秒运行一次
			Timer timer = new Timer();
			timer.schedule(new TimerTask(){
				@Override
				public void run() {						
					try {	
						//当检测到游戏状态为结束时，定时器停止运行
						if(shootInfo.getGameStatus()==ShootClientInfo.GAMEOVER) {
							world.action();
							timer.cancel();
						}
						Object obj = ois.readObject();
						shootInfo=(ShootClientInfo)obj;
						setWorld(shootInfo);						
						world.action();
						oos.reset();
						//发送当前英雄机的信息到服务端
						oos.writeObject(world.getHeroinfo());
						oos.flush();
						//设置当前英雄机的大招属性为false
						world.getHeroinfo().setUltimateSkillFlag(false);
					} catch (ClassNotFoundException | EOFException |SocketException e) {
						if(e instanceof EOFException ||e instanceof SocketException) {
							System.out.println("与服务器断开连接");
							timer.cancel();
						}
					} catch (IOException e) {
						System.out.println("与服务器断开连接");
						//e.printStackTrace();
					}					
				}			
			}, frequency, frequency);
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
	public void addJFrame() {
		JFrame frame = new JFrame();
		frame.add(world);	
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(World.WORLD_WIDTH, World.WORLD_HEIGHT);
		frame.setLocationRelativeTo(null); 
		frame.setVisible(true);
	}
	public void setWorld(ShootClientInfo clinetinfo) {
		world.setHero(clinetinfo.getHero());
		world.setSky(clinetinfo.getSky());
		world.setEnemys(clinetinfo.getEnemy());					
		world.setBullets(clinetinfo.getHeroBullets());
		world.setBulletEnemies(clinetinfo.getBulletEnemies());
		world.setEnemysBoss(clinetinfo.getEnemyBoss());
		world.setBulletBoss(clinetinfo.getBulletBoss());
		world.setUs(clinetinfo.getUltimateSkill());
		world.setWps(clinetinfo.getWingPlanes());
		world.setWingBullets(clinetinfo.getWingBullets());
		switch(clinetinfo.getGameStatus()) {
		case ShootClientInfo.READY: 
			world.setStatus(World.START);
			break;
		case ShootClientInfo.RUN:
			world.setStatus(World.RUN);
			break;
		case ShootClientInfo.GAMEOVER:
			world.setStatus(World.END);
			break;
		}		
	}
	public static void main(String[] args) {
		ShootClient client = new ShootClient();
		client.addJFrame();
		client.start();
	}
}
